package org.jeecg.modules.config;

import cn.hutool.core.util.IdUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.listener.DefaultMessageListener;
import org.jeecg.modules.service.MqttService;
import org.jeecg.modules.service.impl.MqttServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author tanxy
 * @date 2022年11月27日 22:12
 */
@Configuration
public class MqttServiceConfig {

    @Autowired
    private RedisUtil redisUtil;
    @Value("${mqtt.url}")
    private String url ;
    private String clientId = IdUtil.simpleUUID();
    @Value("${mqtt.username}")
    private String userName ;
    @Value("${mqtt.password}")
    private String password ;

    @Bean
    public void mqttService(){
        MqttService mqtt = new MqttServiceImpl(url,clientId,userName,password);
        mqtt.setListener(new DefaultMessageListener(mqtt,redisUtil));
        try {
            mqtt.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
